package cocoaas3.Titanium.UI{
	
	import jp.progression.casts.*;
	import jp.progression.commands.display.*;
	import jp.progression.commands.lists.*;
	import jp.progression.commands.managers.*;
	import jp.progression.commands.media.*;
	import jp.progression.commands.net.*;
	import jp.progression.commands.tweens.*;
	import jp.progression.commands.*;
	import jp.progression.data.*;
	import jp.progression.events.*;
	import jp.progression.loader.*;
	import jp.progression.*;
	import jp.nium.core.debug.Logger;

	import flash.display.*;
	import flash.net.*;
	import flash.geom.*;
	import flash.security.*;
	import flash.system.*;


	public class ImageView extends Sprite{

		public var crop:Boolean = false;

		private var self:ImageView;
		private var image:String;
		private var _imageWidth:uint;
		private var _imageHeight:uint;
		private var loadBD_com:LoadBitmapData;


		public function ImageView( $width:uint, $height:uint, $image:String=null ){
			self = this;
			image = $image;
			_imageWidth = $width;
			_imageHeight = $height;

			if( image ){
				loadImage( image );
			}
		}



		public function loadImage( url:String ){
			self.removeChildren();

			// TODO: Resource を使わずに、ファイルを使ったキャッシュ処理

			var resourceKey:String = url +"width="+ _imageWidth +"&height="+ _imageHeight;
			var list:SerialList = new SerialList();
			var resource:Resource = getResourceById( resourceKey );
			// キャッシュがなければロード
			if( !resource ){
				var context:LoaderContext = new LoaderContext(); 
				context.checkPolicyFile = true; 
				loadBD_com = new LoadBitmapData( new URLRequest(url) );
				loadBD_com.context = context
				list.addCommand(
					loadBD_com,
					function(){
						// リサイズしてキャッシュにつっこむ
						var bd:BitmapData = this.latestData;
						if( crop ){
							var resized_bd:BitmapData = cropAndResize( bd, _imageWidth, _imageHeight );
							new Resource( resourceKey, resized_bd )
							//resized_bd.dispose();
						} else {
							new Resource( resourceKey, bd )
						}
					}
				);
			}

			list.addCommand(
				function(){
					resource = getResourceById( resourceKey );
					var bmp:Bitmap = new Bitmap( resource.toBitmapData() );
					if( loadBD_com ){
						loadBD_com.dispose();
					}
					self.addChild( bmp );
				},
			null);
			list.execute();	
		}


		private function cropAndResize( src:BitmapData, targetWidth:uint, targetHeight:uint ):BitmapData {
			var cropRect = new Rectangle();
			var sourceRatio:Number = src.width / src.height;
			var cropRatio:Number = targetWidth / targetHeight;
			var scaleRatio:Number;
			if ( cropRatio > sourceRatio ) {
				// クロップ範囲のほうが横長の場合
				scaleRatio = targetWidth / src.width;
				cropRect.width = src.width;
				cropRect.height = targetHeight / scaleRatio;
				cropRect.x = 0;
				cropRect.y = src.height / 2 - cropRect.height / 2;
			} else {
				// クロップ範囲のほうが縦の場合
				scaleRatio = targetHeight / src.height;
				cropRect.height = src.height;
				cropRect.width = targetWidth / scaleRatio;
				cropRect.y = 0;
				cropRect.x = src.width / 2 - cropRect.width / 2;
			}
			var cropped_bd:BitmapData = new BitmapData(cropRect.width,cropRect.height);
			cropped_bd.copyPixels(src, cropRect, new Point(0, 0));

			// リサイズ;
			var mat:Matrix = new Matrix();
			mat.scale(scaleRatio, scaleRatio);
			var resized_bd:BitmapData = new BitmapData( targetWidth, targetHeight );
			resized_bd.draw(cropped_bd, mat, null, null, null, true);

			cropped_bd.dispose()

			return resized_bd;
		}



	}
}



